package main

import (
	ginBlog "blog.wuzhispace.com/internal"
	g "blog.wuzhispace.com/internal/global"
	"blog.wuzhispace.com/internal/middleware"
	"flag"
	"github.com/gin-gonic/gin"
	"log"
	"strings"
)

func main() {
	// 读取配置文件
	configPath := flag.String("c", "../config.yml", "配置文件路径")
	flag.Parse()

	conf := g.ReadConfig(*configPath)

	_ = ginBlog.InitLogger(conf)
	db := ginBlog.InitDatabase(conf)
	rdb := ginBlog.InitRedis(conf)

	//	初始化gin服务
	gin.SetMode(conf.Server.Mode)
	r := gin.New()
	r.SetTrustedProxies([]string{"*"})
	//	开发模式使用gin自带的日志和恢复中间件，生产模式使用自定义的中间件
	if conf.Server.Mode == "debug" {
		r.Use(gin.Logger(), gin.Recovery())
	} else {
		r.Use(middleware.Recovery(true), middleware.Logger())
	}
	r.Use(middleware.CORS())
	r.Use(middleware.WithGormDB(db))
	r.Use(middleware.WithRedisDB(rdb))

	r.Use(middleware.WithCookieStore(conf.Session.Name, conf.Session.Salt))
	ginBlog.RegisterHandlers(r)

	// 使用本地文件上传, 需要静态文件服务, 使用七牛云不需要
	if conf.Upload.OssType == "local" {
		r.Static(conf.Upload.Path, conf.Upload.StorePath)
	}

	serverAddr := conf.Server.Port
	if serverAddr[0] == ':' || strings.HasPrefix(serverAddr, "0.0.0.0:") {
		log.Printf("Serving HTTP on (http://localhost:%s/) ... \n", strings.Split(serverAddr, ":")[1])
	} else {
		log.Printf("Serving HTTP on (http://%s/) ... \n", serverAddr)
	}
	r.Run(serverAddr)
}
